package com.youaishujuhui.devops.web;

import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.youaishujuhui.devops.web.domain.game.Company;
import com.youaishujuhui.devops.web.domain.game.Department;
import com.youaishujuhui.devops.web.domain.game.Game;
import com.youaishujuhui.devops.web.domain.host.Host;
import com.youaishujuhui.devops.web.domain.manage.SysResource;
import com.youaishujuhui.devops.web.domain.manage.SysRole;
import com.youaishujuhui.devops.web.domain.manage.SysUser;


//import com.youaishujuhui.devops.gamesys.cache.CacheManager;

/**
 * 
* @Description: spring web 组件启动监听
* @author chendongming
* @date 2018年5月15日 下午8:31:21
*
 */
@Component
public class AppContext  implements ApplicationListener<ContextRefreshedEvent> {
	
	private Logger log = LoggerFactory.getLogger(AppContext.class);
	
	public static ApplicationContext applicationContext;

//@Autowired
//private CacheManager cacheManager;
	
    @Autowired
    private Dao nutDao;
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		log.info("web init..........");
		
		initDB();
		
		setContext(event);

//cacheManager.init();
		/*
		try {
			sysManager.init();
		} catch (Exception e) {
			log.error(e.toString());
			e.printStackTrace();
		}*/
	}

	private void initDB() {
		// 建表
        Daos.createTablesInPackage(nutDao, "com.youaishujuhui.devops.web.domain", false);
        Daos.migration(nutDao, "com.youaishujuhui.devops.web.domain", true, false, true);
        
//        SysUser user = nutDao.fetch(SysUser.class, 1);
//        
//        List<SysRole> roles = new ArrayList<SysRole>();
//        
//        SysRole role1 = new SysRole();
//        role1.setCode("admin");
//        role1.setName("超级管理员");
//        
//        SysRole role2 = new SysRole();
//        role2.setCode("yunying");
//        role2.setName("运营用户");
//        
//        roles.add(role1);
//        roles.add(role2);
//        
//        user.setRoles(roles);
//        
//        nutDao.insertLinks(user, "roles");
        
//        SysUser user = nutDao.fetch(SysUser.class, 1);
//        nutDao.fetchLinks(user, "roles");
//        
//        if (user.getRoles() != null) {
//        	for (SysRole role : user.getRoles()) {
//        		System.out.println(role.getName());
//        	}
//        }

//        SysResource resource = new SysResource();
//        resource.setResId(5000);
//        resource.setPId(0);
//        resource.setUri("/manage");
//        resource.setName("系统管理");
//        
//        SysResource resource1 = new SysResource();
//        resource1.setResId(5001);
//        resource1.setPId(5000);
//        resource1.setUri("/manage/page");
//        resource1.setName("系统管理页面");
//        
//        SysResource resource2 = new SysResource();
//        resource2.setResId(5400);
//        resource2.setPId(5000);
//        resource2.setUri("/manage/user");
//        resource2.setName("用户管理");
//        
//        SysResource resource3 = new SysResource();
//        resource3.setResId(5401);
//        resource3.setPId(5000);
//        resource3.setUri("/manage/user/page");
//        resource3.setName("用户管理页面");
//        
//        SysResource resource5 = new SysResource();
//        resource5.setResId(5402);
//        resource5.setPId(5000);
//        resource5.setUri("/manage/user/query");
//        resource5.setName("用户管理查询");
//        
//        //nutDao.fastInsert(resource);
//        //nutDao.fastInsert(resource1);
//        
//        List<SysResource> resources = new ArrayList<SysResource>();
//        
//        SysRole role = nutDao.fetch(SysRole.class, 1);
//        
//        resources.add(resource1);
//        resources.add(resource);
//        resources.add(resource2);
//        resources.add(resource3);
//        //resources.add(resource4);
//        resources.add(resource5);
//        
//        role.setResources(resources);
//        
//        nutDao.insertLinks(role, "resources");
	}
	
	private void setContext(ContextRefreshedEvent event) {
		applicationContext = event.getApplicationContext();
	}

}
